package com.hspedu.properties;

import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Properties;

/**
 * @Author Stringzhua
 * @Date 2024/3/17 19:52
 * description:
 */
public class Properties03 {
    public static void main(String[] args) throws IOException {
        //使用Properties 类来创建 配置文件,修改配置文件
        Properties properties = new Properties();
        //创建
        //1.如果该文件没有key就是创建
        //2.如果该文件有key,就是修改|替换
        /*
         Properties 父类是 Hashtable,底层就是Hashtable核心方法
         public synchronized V put(K key, V value) {
                // Make sure the value is not null
                if (value == null) {
                    throw new NullPointerException();
                }

                // Makes sure the key is not already in the hashtable.
                Entry<?,?> tab[] = table;
                int hash = key.hashCode();
                int index = (hash & 0x7FFFFFFF) % tab.length;
                @SuppressWarnings("unchecked")
                Entry<K,V> entry = (Entry<K,V>)tab[index];
                for(; entry != null ; entry = entry.next) {
                    if ((entry.hash == hash) && entry.key.equals(key)) {
                        V old = entry.value;
                        entry.value = value;//如果key存在,就替换旧值
                        return old;
                    }
                }

                addEntry(hash, key, value, index);//如果是新值,就addEntry
                return null;
            }
        * */
        properties.setProperty("charset", "utf8");
        properties.setProperty("user", "汤姆");//保存时是中文的unicode编码
        properties.setProperty("password", "8888888");//修改某个k-v键值对

        //将k-v存储到文件中
        properties.store(new FileOutputStream("chapter19/src/com/hspedu/mysql2.properties"), "hello,world");
        //helloworld是properties文件的注释
        System.out.println("配置文件保存成功");
    }
}
